package com.TeamJigsaw.DanteTrail.Presenters;
import java.util.List;

import com.TeamJigsaw.DanteTrail.Debug.Debug;
import com.TeamJigsaw.DanteTrail.Models.AmmoItem;
import com.TeamJigsaw.DanteTrail.Models.AxleItem;
import com.TeamJigsaw.DanteTrail.Models.ClothingItem;
import com.TeamJigsaw.DanteTrail.Models.FoodItem;
import com.TeamJigsaw.DanteTrail.Models.OxenItem;
import com.TeamJigsaw.DanteTrail.Models.Party;
import com.TeamJigsaw.DanteTrail.Models.Oxen;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.TeamJigsaw.DanteTrail.Models.StoreItem;
import com.TeamJigsaw.DanteTrail.Models.TongueItem;
import com.TeamJigsaw.DanteTrail.Models.WheelItem;
import com.TeamJigsaw.DanteTrail.Views.MainViewFrame;
import com.TeamJigsaw.DanteTrail.Views.Interfaces.ShopViewInterface;
import com.TeamJigsaw.DanteTrail.Presenters.TownViewController;

import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Represents a ShopView Controller
 * @author Victor Lee
 */
public class ShopViewController implements ViewController {
	private final int DEFAULT_VALUE = 100;
	
	private Party party;
	
	private MainViewFrame mainView;
	
	private ShopViewInterface shopview;
	
	private TownViewController townview;
	
	private List<StoreItem> itemList;
	
	private int[] buyList;
	
	private static double shopMultiply = 1;
	
	
	/**
	 * Creates and initializes the ShopViewController
	 * @param party The party that is playing the game
	 * @param mainView The main view of the user interface
	 */
	public ShopViewController(Party party, MainViewFrame mainView) {
		this.party = party;
		this.mainView = mainView;
		townview = null;
		
		//How do I get the shopview for below? MainFrame doesn't have it.
		shopview = this.mainView.getShop();
		
		itemList = new ArrayList<StoreItem>();

		//initialize the item list
		itemList.add(new OxenItem(DEFAULT_VALUE));
		itemList.add(new FoodItem(DEFAULT_VALUE));
		itemList.add(new ClothingItem(DEFAULT_VALUE));
		itemList.add(new AmmoItem(DEFAULT_VALUE));
		itemList.add(new WheelItem(DEFAULT_VALUE));
		itemList.add(new AxleItem(DEFAULT_VALUE));
		itemList.add(new TongueItem(DEFAULT_VALUE));
		
		buyList = new int[itemList.size()];
		
		
		//Set prices for all the items
		shopview.setOxenPrice(40 * shopMultiply);
		shopview.setFoodPrice(1 * shopMultiply);
		shopview.setClothesPrice(10 * shopMultiply);
		shopview.setAmmoPrice(2 * shopMultiply);
		shopview.setWheelsPrice(10 * shopMultiply);
		shopview.setAxelsPrice(10 * shopMultiply);
		shopview.setTonguesPrice(10 * shopMultiply);
		shopMultiply += .1;
		
		//initialize the buy list
		getBuyList();
		
		//create the listeners
		createListeners();
		
		//intialize UI values to proper values
		shopview.setCost(getTotalCost());
		shopview.setInventoryWeight(party.getWeight(), party.getMaxWeight());
		shopview.setBuyWeight(getTotalWeight());
		
	}
	
	/**
	 * Creates and initializes the ShopViewController
	 * @param party The party that is playing the game
	 * @param mainView The main view of the user interfaceo
	 * @param townview The TownViewController that originally called the ShopViewController
	 */
	public ShopViewController(Party party, MainViewFrame mainView, TownViewController townview) {
		this.party = party;
		this.mainView = mainView;
		this.townview = townview;
		Debug.println("In the constructor of ShopViewController :  "+townview, Debug.M_NINE_DEMO);
		//How do I get the shopview for below? MainFrame doesn't have it.
		shopview = this.mainView.getShop();
		
		itemList = new ArrayList<StoreItem>();

		//initialize the item list
		itemList.add(new OxenItem(DEFAULT_VALUE));
		itemList.add(new FoodItem(DEFAULT_VALUE));
		itemList.add(new ClothingItem(DEFAULT_VALUE));
		itemList.add(new AmmoItem(DEFAULT_VALUE));
		itemList.add(new WheelItem(DEFAULT_VALUE));
		itemList.add(new AxleItem(DEFAULT_VALUE));
		itemList.add(new TongueItem(DEFAULT_VALUE));
		
		buyList = new int[itemList.size()];
		
		double multiply = shopMultiply; 
		//Set prices for all the items
		shopview.setOxenPrice(40 * multiply);
		shopview.setFoodPrice(1 * multiply);
		shopview.setClothesPrice(10 * multiply);
		shopview.setAmmoPrice(2 * multiply);
		shopview.setWheelsPrice(10 * multiply);
		shopview.setAxelsPrice(10 * multiply);
		shopview.setTonguesPrice(10 * multiply);
		multiply += .1;
		getBuyList();		
		//initialize the buy list

		
		//create the listeners
		createListeners();
		
		//intialize UI values to proper values
		shopview.setCost(getTotalCost());
		shopview.setInventoryWeight(party.getWeight(), party.getMaxWeight());
		shopview.setBuyWeight(getTotalWeight());
	}
	
	/**
	 * Sets the inventory jLabels to current values
	 */
	public void setInventoryLabels(){
		shopview.setMoney(party.getMoney());
		shopview.setAmmo(party.getAmmo());
		shopview.setFood(party.getFood());
		shopview.setClothes(party.getClothes());
		shopview.setOxen(party.getWagon().getOxen().size());
		shopview.setWheels(party.getWheels());
		shopview.setTongues(party.getTongue());
		shopview.setAxels(party.getAxels());
	}

	/**
	 * Gest the values of the buy list of items
	 */
	public void getBuyList() {
		buyList[OxenItem.ID] = shopview.getOxenAmount();
		buyList[FoodItem.ID] = shopview.getFoodAmount();
		buyList[ClothingItem.ID] = shopview.getClothesAmount();
		buyList[AmmoItem.ID] = shopview.getAmmoAmount();
		buyList[WheelItem.ID] = shopview.getWheelAmount();
		buyList[AxleItem.ID] = shopview.getAxelAmount();
		buyList[TongueItem.ID] = shopview.getTongueAmount();
	}
	
	/**
	 * Gets the total cost of all the items in the buy list
	 * @return The total cost of all the items in the buy list
	 */
	public int getTotalCost() {
		int cost = 0;
		//calculate total cost of the items
		for (int i = 0; i < itemList.size(); i++) {
			cost += itemList.get(i).getItemCost() * buyList[i];
		}
		
		return cost;
	}

	/**
	 * Gets the total weight of all the items in the buy list
	 * @return The total weight of all the items in the buy list
	 */
	public int getTotalWeight() {
		int weight = 0;
		//calculate total cost of the items
		for (int i = 0; i < itemList.size(); i++) {
			weight += itemList.get(i).getItemWeight() * buyList[i];
		}
		
		return weight;
	}
	
	/**
	 * Creates the listeners for all the components in the view
	 */
	@Override
	public void createListeners() {
		//check to see if listeners already exist for the view
		if (shopview.listenersExist() == true)
			return;
		
		//ActionListener for the Buy Button that completes all the buying
		Debug.println("createListeners() called", Debug.M_SIX_DEMO);
		ActionListener buy = new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				
				//get what is going to be purchased
				getBuyList();
				
				//check to see if the itemlist and buy list are matching
				if (itemList.size() != buyList.length) {
					Debug.println("ERROR: ITEM LIST AND BUY LIST DO NOT MATCH", 
							Debug.WARNING);
					return;
				}
				
				//calculate total cost of the items
				int cost = getTotalCost();
				 
				//does the player have enough money?
				if(party.getMoney() > cost) {
					
					//does the player have enough space in the wagon?
					if (party.getWeight() + getTotalWeight() <= party.getMaxWeight()) {
						
						//add all the oxen to the wagon
						for(int x = 0; x < itemList.get(0).getItemAmount()*buyList[0]; x++) {
							Oxen oxen = new Oxen();
							party.getWagon().addOxen(oxen);
						}
						
						Debug.println("OLD FOOD: " + party.getFood(), Debug.INFO);
						Debug.println("LISTEN HERE: " + buyList[FoodItem.ID] + " " + itemList.get(FoodItem.ID).getItemAmount(), Debug.INFO);
						Debug.println("Things being added: " + buyList[FoodItem.ID] * itemList.get(FoodItem.ID).getItemAmount(), Debug.INFO);
						Debug.println("NEW FOOD: " + (party.getFood() + buyList[FoodItem.ID] * itemList.get(FoodItem.ID).getItemAmount()), Debug.INFO);
						
						//add all the items to the party
						party.addFood(buyList[FoodItem.ID] * itemList.get(FoodItem.ID).getItemAmount());
						party.addClothes(buyList[ClothingItem.ID] * itemList.get(ClothingItem.ID).getItemAmount());
						party.addAmmo(buyList[AmmoItem.ID] * itemList.get(AmmoItem.ID).getItemAmount());
						party.addWheels(buyList[WheelItem.ID] * itemList.get(WheelItem.ID).getItemAmount());
						party.addAxels(buyList[AxleItem.ID] * itemList.get(AxleItem.ID).getItemAmount());
						party.addTongues(buyList[TongueItem.ID] * itemList.get(TongueItem.ID).getItemAmount());
						party.addWeight(getTotalWeight());
						
						//subtract the money
						party.addMoney(-cost);
						
						//Testing Button Values
						Debug.println("Food amount is "+ shopview.getFoodAmount(), Debug.INFO);
						Debug.println("Clothes amount is "+ shopview.getClothesAmount(), Debug.INFO);
						Debug.println("Ammo amount is "+ shopview.getAmmoAmount(), Debug.INFO);
						Debug.println("Wheels amount is "+ shopview.getWheelAmount(), Debug.INFO);
						Debug.println("Axels amount is "+ shopview.getAxelAmount(), Debug.INFO);
						Debug.println("Tongue amount is "+ shopview.getTongueAmount(), Debug.INFO);
						Debug.println("", Debug.INFO);
						
					}
					else {
						//notify the player that he/she does not have enough space
						JOptionPane.showMessageDialog(mainView, "You do not have " +
								"enough space in your wagon to purchase these items. " +
								"Please remove some items.");
					}
					shopview.resetValues();
				}
				else {
					//notify player that he/she does not have enough money
					JOptionPane.showMessageDialog(mainView, "You do not have " +
							"enough money to purchase these items. Please remove " +
							"some items.");
				}
				
				setInventoryLabels();
				shopview.setInventoryWeight(party.getWeight(), party.getMaxWeight());
			}
		};
		shopview.setBuyListener(buy);
		
		ActionListener backListen = new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e){
					if(townview == null){
						IntroviewController intro = new IntroviewController(party, mainView);
						((CardLayout) mainView.getContentPane().getLayout()).show(mainView.getContentPane(), "Profession");
						mainView.setTitle("Profession");
						
						party.getWagon().getOxen().clear();
						party.setFood(0);
						party.setClothes(0);
						party.setAmmo(0);
						party.setWheels(0);
						party.setAxels(0);
						party.setTongues(0);
						party.setWeight(0);
						party.setMoney(0);
						
						System.out.println(party.toString());
						
						for(int x = 0; x < buyList.length; x++){
							buyList[x] = 0;
						}
					}
					else{
						//Sets the nextbutton to go back to the TownView instead of going back to the GameView
						((CardLayout) mainView.getContentPane().getLayout()).show(mainView.getContentPane(), "Town");
						mainView.setTitle(townview.getName());
						Debug.println("In next button of shopviewcontroller townview is set: "+townview, Debug.M_NINE_DEMO);
						mainView.getShop().removeListeners();
					}
				
				}
			};
		shopview.setBackListener(backListen);
		// TODO Auto-generated method stub
		
		ChangeListener costListen = new ChangeListener(){
			@Override
			public void stateChanged(ChangeEvent e) {
				getBuyList();
				shopview.setCost(getTotalCost());
				shopview.setBuyWeight(getTotalWeight());
			}
			
		};
		
		shopview.setCostListener(costListen);
		
		ActionListener nextListen = new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				
				Debug.println(party.toString(), Debug.M_SIX_DEMO);
				if(townview == null){
					((CardLayout) mainView.getContentPane().getLayout()).show(mainView.getContentPane(), "Game");
					mainView.setTitle("Trail");
					GameViewController gvc = new GameViewController(party, mainView);
					Debug.println("In next button of shopviewcontroller townview == null: "+townview, Debug.M_NINE_DEMO);
					mainView.getShop().removeListeners();
				}
				else{
					//Sets the nextbutton to go back to the TownView instead of going back to the GameView
					((CardLayout) mainView.getContentPane().getLayout()).show(mainView.getContentPane(), "Town");
					mainView.setTitle(townview.getName());
					Debug.println("In next button of shopviewcontroller townview is set: "+townview, Debug.M_NINE_DEMO);
					mainView.getShop().removeListeners();
				}
				
			}
		};
		shopview.setNextListener(nextListen);
	}
	
	/**
	 * Returns a string representation of the ShopViewController
	 * @return A string representation of the ShopViewController
	 */
	@Override
	public String toString()
	{
		return ("This is a ShopViewController");
	}
}
